using System.Data;
using System.Linq;

namespace Home.Site.Models.DataLayer
{
    public class OfferRepository : IOfferRepository
    {
        readonly PromikeDBEntities context = new PromikeDBEntities();

        public IQueryable<Offer> All { get { return context.Offers; } }
        public Offer Find(int id)
        {
            return context.Offers.Find(id);
        }

        public void InsertOrUpdate(Offer offer)
        {
            if (offer.idoffer == default(int))
            {
                // New entity
                context.Offers.Add(offer);
            }
            else
            {
                // Existing entity
                context.Entry(offer).State = EntityState.Modified;
            }
        }

        public void Delete(int id)
        {
            var offer = context.Offers.Find(id);
            context.Offers.Remove(offer);
        }

        public void Save()
        {
            context.SaveChanges();
        }

        public void Dispose()
        {
            context.Dispose();
        }
    }
}